#include "server.h"

typedef int (*_P_FUNC_SVC)(char *, char *);
#define _SVC_FUNC(_svc)   \
static int _SVC_FUNC_##_svc(char *peer_ip, char *body)

_SVC_FUNC(HELLO)
{/*{{{*/
    errlog("%s: ok", __func__);
    return(atoi(SOAR_ERR_OK));
}/*}}}*/

_SVC_FUNC(LOG)
{/*{{{*/
    char para_desc[100];
    PARA para;
    char program[NAME_SIZE+1];
    char *content;
    int res;
    int offset;
    char file_nm[LONG_FILE_NM_SIZE+1];
    char *resp;

    sprintf(para_desc, "program(string:1-%d) content(string:1-%d)",
            NAME_SIZE, BUFFER_SIZE);
    if((res=para_load(body, para_desc, &para)))
    {
        if(res > 0)
            errlog("%s: (%s) no.%d para invalid",
                    __func__, body, res);
        else
            errlog("%s: para_load fail, res:%d",
                    __func__, res);
        return(atoi(SOAR_ERR_PARA));
    }
    para_copy_by_name(para, "program", program);
    trim_head_space(program); trim_tail_space(program);
    para_ref_by_name(para, "content", &content);

    offset=sprintf(file_nm, "%s/%s",
            share_info->_customlog_.location, peer_ip);
    if(create_dir(file_nm) == 0)
    {
        offset+=sprintf(file_nm+offset, "/%s", program);
        if(create_dir(file_nm) == 0)
        {
            char now[TIMESTAMP_SIZE+20+1];
            FILE *fp;

            curr_timestamp_str(now);
            sprintf(file_nm+offset, "/%.8s", now);
            timestamp_fmt(now, NULL, now, NULL);
            if((fp=fopen(file_nm, "a")))
            {
                fprintf(fp, "%s -- %s\n", now, content);
                fclose(fp);
                resp=SOAR_ERR_OK;
            }
            else
            {
                errlog("%s: cannot open log file(%s)",
                        __func__, file_nm);
                resp=SOAR_ERR_LOG_OPEN_FILE_FAIL;
            }
        }
        else
        {
            errlog("%s: cannot create program dir(%s)",
                    __func__, file_nm);
            resp=SOAR_ERR_LOG_MKDIR_PROGRAM_FAIL;
        }
    }
    else
    {
        errlog("%s: cannot create ip dir(%s)",
                __func__, file_nm);
        resp=SOAR_ERR_LOG_MKDIR_IP_FAIL;
    }

    para_free(para);

    return(atoi(resp));
}/*}}}*/

int svc_call(char *peer_ip, char *msg_in)
{/*{{{*/
    static struct
    {
        char *svc;
        _P_FUNC_SVC p_svc_func;
    } svc_map[]=
    {
        { "LOG", _SVC_FUNC_LOG },
        { "HELLO", _SVC_FUNC_HELLO },
        { NULL, NULL }
    };
    char *body;
    char *svc;
    int res;
    int idx;
    int rtn;

    body=msg_in;
    if((svc=para_get_item(msg_in, &body)) == NULL ||
            (res=strlen(svc)) <= 0 || res > NAME_SIZE)
    {
        errlog("%s %s: svc(%s) invalid",
                __FILE__, __func__, (svc)?svc:SPACES);
        return(atoi(SOAR_ERR_SVC_NOT_FOUND));
    }

    for(idx=0; svc_map[idx].svc; idx++)
        if(strcasecmp(svc_map[idx].svc, svc) == 0) break;
    if(svc_map[idx].svc)
    {
        rtn=svc_map[idx].p_svc_func(peer_ip, body);
    }
    else
    {
        errlog("%s %s: proc_(%s) not found",
                __FILE__, __func__, svc);
        rtn=atoi(SOAR_ERR_SVC_NOT_FOUND);
    }

    return(rtn);
}/*}}}*/
